package br.ufc.storm.WServices;

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import br.ufc.storm.Control.DBHandler;
import br.ufc.storm.Control.XMLHandler;
import br.ufc.storm.jaxb.AbstractComponentType;
import br.ufc.storm.jaxb.ContextContract;

public class CoreServices {
	
	/**
	 * 	 * This method adds an abstract component into database.
	 * @param cmp(xml file as string)
	 * @return True if no exception occurs
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	public boolean addAbstractComponent(String cmp) throws ParserConfigurationException, SAXException, IOException{
		return XMLHandler.addAbstractComponentFromXML(new File(cmp));
	}
	
	/**
	 * This method search for an abstract component and returns its description.
	 * @param name Abstract component name
	 * @return Abstract component data as string
	 */
	
	public String getAbstractComponent(String name){
		return XMLHandler.getAbstractComponent(name);
	}
	/**
	 * This method search an abstract component and returns it inner components.	
	 * @param id Abstract component id
	 * @return Abstract component data as string
	 */
	
	public String getAbstractComponentByID(int id){
		AbstractComponentType ac = DBHandler.getAbstractComponent(DBHandler.getAbstractComponentName(id));
		return XMLHandler.getAbstractComponent(ac.getName());//ac.export();
	}
	
	/**
	 * This method adds an profile into database.
	 * @param cmp XML file as string
	 * @return True if no exception occurs
	 */
	public boolean addProfile(String cmp) throws ParserConfigurationException, SAXException, IOException{
		return XMLHandler.addProfileFromXML(cmp);
	}
	
	/**
	 * This method adds an instantiation type into database.
	 * @param cmp XML file as string
	 * @return True if no exception occurs
	 */
	public boolean addInstantiationType(String cmp){
		int id = XMLHandler.addInstantiationTypeFromXML(XMLHandler.openXMLFileNew(cmp));
		if(id > 0){
			return true;
		}else{
			return false;
		}
		
	}
	/**
	 * This method adds an inner component into database.
	 * @param cmp XML file as string
	 * @return True if no exception occurs
	 */
	public boolean addInnerComponent(String cmp){
		boolean id = XMLHandler.addInnerComponentFromXML(XMLHandler.openXMLFileNew(cmp));
			return id;
	}
	
	/**
	 * This method adds a context parameter into database.
	 * @param cmp XML file as string
	 * @return True if no exception occurs
	 */
	public boolean addContextParameter(String cmp){
		return XMLHandler.addContextParameterFromXML(XMLHandler.openXMLFileNew(cmp));
	}
	
	/**
	 * This method gets a list of context parameters.
	 * @param name Abstract component name
	 * @return XML file with a list of context parameters belonging to given abstract component
	 */
	public String getContextParameter(String name){
		return XMLHandler.getContextParameters(name);
	}
	
	/**
	 * This method adds a concrete component into core.
	 * @param abstractcomponent Abstract component name.
	 * @param url Source code file URL
	 * @return True if no exception occurs
	 */
	public boolean addConcreteComponent(String abstractcomponent, String url){
		return false;
		
	}
	
	
	
	/**
	 * This method adds an unit to an concrete component
	 * @return True if no exception occurs
	 */
	public int addConcreteUnit(String cmp){
		return XMLHandler.addUnitFromXML(XMLHandler.openXMLFileNew(cmp));
	}
	
/**
 * 
 * @param data
 * @param xml
 * @return
 */
	 public boolean addUnitFile(byte[] data, String xml) {
		if(XMLHandler.addUnitFile(xml, data)){
			return true;
		}else{
			return false;
		}

   }

	
	/**
	 * Set a component as obsolete, disabling it for new components, but preserving compatibility with old components
	 * @param cmp Abstract component name
	 * @return True if operation is well successful
	 */
	public boolean setObsolete(String cmp){
		if(DBHandler.setObsolete(cmp)){
			return true;
		}else{
			return false;
		}

	}

	/**
	 * This method lists all components registered on database
	 * @return - XML file with a list of all components
	 */
	public String list(){
		String s = XMLHandler.listComponent();
		return s;		
	}
	
	/**
	 * This method resolve a context contract
	 * @param cmp Context contract
	 * @return List of components
	 */
	public List<ContextContract> resolve(String cmp){
		return (List<ContextContract>) XMLHandler.resolve(XMLHandler.openXMLFileNew(cmp));
	}
	/**
	 * This method deploy the application in selected backend infrastructure.
	 * @param cmp - component in xml as string
	 */
	public void deploy(String cmp){

	}

  
	

}
